<template>
	<div>
		<!-- 风险评估弹窗 -->
    <el-dialog 
      title="风险评估"
      append-to-body
      :visible.sync="riskAssessDialog"
      :close-on-click-modal="false" 
      :before-close="handleCloseRiskAssess"
      width="60%"
    >
      <el-form :model="assessForm" :rules="assessRules" ref="assessForm">
        <div class="risk_assess_basic">
          <el-row>

            <!-- 评估人 -->
            <el-col :span="12">
              <el-form-item label="评估人" prop="assessUser">
                <el-select 
                  v-model="assessForm.assessUser" 
                  clearable 
                  placeholder="请选择评估人"
                  size="small"
                >
                  <el-option
                    v-for="user in userList"
                    :key="user.id"
                    :label="user.realName"
                    :value="user.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 评估时间 -->
            <el-col :span="12">
              <el-form-item label="评估时间" prop="assessTime">
                <el-date-picker
                  v-model="assessForm.assessTime"
                  type="date"
                  placeholder="选择评估日期"
                  size="small">
                </el-date-picker>
              </el-form-item>
            </el-col>

          </el-row>
        </div>

        <!-- 评估方法 -->
        <div class="risk_assess_method">
          <el-row>
            <el-col :span="6"></el-col>
            <el-col :span="12">
              <span class="assess_method_span">选择评估方法</span>
              <el-radio-group v-model="assessForm.assessMethod">
                <el-radio-button :label="lecLabel"></el-radio-button>
                <el-radio-button :label="lsLabel"></el-radio-button>
              </el-radio-group>
            </el-col>
            <el-col :span="6"></el-col>
          </el-row>
        </div>

        <!-- LEC评分 -->
        <div class="lec_assess_detail" v-if="assessForm.assessMethod === lecLabel">
          <el-row>

            <!-- LEC:L -->
            <el-col :span="8">
              <div class="title_div">
                <span class="important_icon">*</span>
                <span class="method_label">可能性（L）</span>
              </div>
              <el-radio-group v-model="lec.l">
                <div 
                  class="radio_div" 
                  v-for="item in lec_l_list"
                  :key="item.dictKey"
                >
                  <el-radio :label="item.dictKey">{{item.dictValue}}</el-radio>
                </div>
              </el-radio-group>
            </el-col>
            
            <!-- LEC:E -->
            <el-col :span="8">
              <div class="title_div">
                <span class="important_icon">*</span>
                <span class="method_label">暴露频率（E）</span>
              </div>
              <el-radio-group v-model="lec.e">
                <div 
                  class="radio_div" 
                  v-for="item in lec_e_list"
                  :key="item.dictKey"
                >
                  <el-radio :label="item.dictKey">{{item.dictValue}}</el-radio>
                </div>
              </el-radio-group>
            </el-col>

            <!-- LEC:C -->
            <el-col :span="8">
              <div class="title_div">
                <span class="important_icon">*</span>
                <span class="method_label">严重性（C）</span>
              </div>
              <el-radio-group v-model="lec.c">
                <div 
                  class="radio_div" 
                  v-for="item in lec_c_list"
                  :key="item.dictKey"
                >
                  <el-radio :label="item.dictKey">{{item.dictValue}}</el-radio>
                </div>
              </el-radio-group>
            </el-col>

          </el-row>
        </div>

        <!-- LS评分 -->
        <div class="ls_assess_detail" v-if="assessForm.assessMethod === lsLabel">
          <el-row>

            <!-- LS:L -->
            <el-col :span="12">
              <div class="title_div">
                <span class="important_icon">*</span>
                <span class="method_label">可能性（L）</span>
              </div>
              <el-radio-group v-model="ls.l">
                <div 
                  class="radio_div" 
                  v-for="item in ls_l_list"
                  :key="item.dictKey"
                >
                  <el-radio :label="item.dictKey">{{item.dictValue}}</el-radio>
                </div>
              </el-radio-group>
            </el-col>

            <!-- LS:S -->
            <el-col :span="12">
              <div class="title_div">
                <span class="important_icon">*</span>
                <span class="method_label">严重性（S）</span>
              </div>
              <el-radio-group v-model="ls.s">
                <div 
                  class="radio_div" 
                  v-for="item in ls_s_list"
                  :key="item.dictKey"
                >
                  <el-radio :label="item.dictKey">{{item.dictValue}}</el-radio>
                </div>
              </el-radio-group>
            </el-col>

          </el-row>
        </div>

        <!-- LEC评分表格 -->
        <div class="lec_assess_table" v-if="assessForm.assessMethod === lecLabel">
          <table>
            <tr>
              <td></td>
              <td><div class="risk_level_a_div">A (&gt;=320)</div></td>
              <td><div class="risk_level_b_div">B (160~319)</div></td>
              <td><div class="risk_level_c_div">C (71~159)</div></td>
              <td><div class="risk_level_d_div">D (&lt;=70)</div></td>
            </tr>
            <tr>
              <td><p>评分</p><span>(L*E*C)</span></td>
              <td>
                <div class="risk_level_a_div" v-if="formatRiskLevelValue( lec.l * lec.e * lec.c ) >= 320">
                  <span>{{formatRiskLevelValue( lec.l * lec.e * lec.c )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_b_div" v-if="formatRiskLevelValue( lec.l * lec.e * lec.c ) >= 160 && formatRiskLevelValue( lec.l * lec.e * lec.c ) <= 319">
                  <span>{{formatRiskLevelValue( lec.l * lec.e * lec.c )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_c_div" v-if="formatRiskLevelValue( lec.l * lec.e * lec.c ) >= 71 && formatRiskLevelValue( lec.l * lec.e * lec.c ) <= 159">
                  <span>{{formatRiskLevelValue( lec.l * lec.e * lec.c )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_d_div" v-if="formatRiskLevelValue( lec.l * lec.e * lec.c ) <= 70">
                  <span>{{formatRiskLevelValue( lec.l * lec.e * lec.c )}}</span>
                </div>
              </td>
            </tr>
          </table>
        </div>

        <!-- LS评分表格 -->
        <div class="ls_assess_table" v-if="assessForm.assessMethod === lsLabel">
          <table>
            <tr>
              <td></td>
              <td><div class="risk_level_a_div">A (&gt;=17)</div></td>
              <td><div class="risk_level_b_div">B (13~16)</div></td>
              <td><div class="risk_level_c_div">C (9~12)</div></td>
              <td><div class="risk_level_d_div">D (&lt;=8)</div></td>
            </tr>
            <tr>
              <td><p>评分</p><span>(L*S)</span></td>
              <td>
                <div class="risk_level_a_div" v-if="formatRiskLevelValue( ls.l * ls.s ) >= 17">
                  <span>{{formatRiskLevelValue( ls.l * ls.s )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_b_div" v-if="formatRiskLevelValue( ls.l * ls.s ) >= 13 && formatRiskLevelValue( ls.l * ls.s ) <= 16">
                  <span>{{formatRiskLevelValue( ls.l * ls.s )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_c_div" v-if="formatRiskLevelValue( ls.l * ls.s ) >= 9 && formatRiskLevelValue( ls.l * ls.s ) <= 12">
                  <span>{{formatRiskLevelValue( ls.l * ls.s )}}</span>
                </div>
              </td>
              <td>
                <div class="risk_level_d_div" v-if="formatRiskLevelValue( ls.l * ls.s ) <= 8">
                  <span>{{formatRiskLevelValue( ls.l * ls.s )}}</span>
                </div>
              </td>
            </tr>
          </table>
        </div>

        <!-- 按钮 -->
        <div class="risk_assess_footer">
          <el-button 
            type="primary"
            size="small"
            @click="saveRiskAssess()"
            :loading="loadSaveRiskAssess">
            保 存
          </el-button>
          <el-button 
            size="small"
            @click="cancelSaveRiskAssess()">
            取 消
          </el-button>
        </div>

      </el-form>
    </el-dialog>
	</div>
</template>

<script>
import { 
  getLECL, 
  getLECE, 
  getLECC, 
  getLSL, 
  getLSS, 
  getUserList, 
  addRiskAssess } from "@/api/saferisk/riskpoint";

export default {
  props: {
    riskAssessDialog: {default: false},
    selectRiskId: {default: ""}
  },
  data() {
    return {
      lecLabel: "LEC",
      lsLabel: "LS",
      assessForm: {
        assessUser: "",
        assessTime: "",
        assessMethod: "LEC"
      },
      assessRules: {
        assessUser: [{ required: true, message: '请选择评估人', trigger: 'change' }],
        assessTime: [{ required: true, message: '请选择评估时间', trigger: 'change' }]
      },
      userList: [],
      lec: {
        l: "",
        e: "",
        c: ""
      },
      ls: {
        l: "",
        s: ""
      },
      lec_l_list: [],
      lec_e_list: [],
      lec_c_list: [],
      ls_l_list: [],
      ls_s_list: [],
      loadSaveRiskAssess: false
    }
  },
  watch: {
    riskAssessDialog(){
      if(this.riskAssessDialog === true){
        this.initData();
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化评估历史弹窗
    initData() {
      this.getAssessDetails();
      this.getUsers();
      this.loadSaveRiskAssess = false;
      this.assessForm = {
        assessUser: "",
        assessTime: "",
        assessMethod: this.lecLabel
      };
      this.lec = {
        l: "",
        e: "",
        c: ""
      };
      this.ls = {
        l: "",
        s: ""
      };
    },

    // 获取lec/ls字典
    getAssessDetails() {
      getLECL().then(res => {
        this.lec_l_list = res.data.data;
      });

      getLECE().then(res => {
        this.lec_e_list = res.data.data;
      });

      getLECC().then(res => {
        this.lec_c_list = res.data.data;
      });

      getLSL().then(res => {
        this.ls_l_list = res.data.data;
      });

      getLSS().then(res => {
        this.ls_s_list = res.data.data;
      });
    },

    // 获取人员列表
    getUsers() {
      getUserList().then(res => {
        this.userList = res.data.data;
      });
    },

    // 保存风险评估
    saveRiskAssess() {
      if(this.assessForm.assessUser === "" || this.assessForm.assessUser === undefined) {
        this.$message.warning("请选择评估人!");
        return;
      }
      if(this.assessForm.assessTime === "" || this.assessForm.assessTime === undefined) {
        this.$message.warning("请选择评估时间!");
        return;
      }
      if(this.assessForm.assessMethod === "") {
        this.$message.warning("请选择评估方法");
        return;
      }
      if(this.assessForm.assessMethod === this.lecLabel) {
        if(this.lec.l === "") {
          this.$message.warning("请选择可能性（L）");
          return;
        }
        if(this.lec.e === "") {
          this.$message.warning("请选择暴露频率（E）");
          return;
        }
        if(this.lec.c === "") {
          this.$message.warning("请选择严重性（C）");
          return;
        }
      }
      if(this.assessForm.assessMethod === this.lsLabel) {
        if(this.ls.l === "") {
          this.$message.warning("请选择可能性（L）");
          return;
        }
        if(this.ls.s === "") {
          this.$message.warning("请选择严重性（S）");
          return;
        }
      }
      this.loadSaveRiskAssess = true;
      var insertData = {
        riskManagementId: this.selectRiskId,
        pgr: this.assessForm.assessUser,
        pgsj: this.assessForm.assessTime !== undefined && this.assessForm.assessTime !== "" && typeof this.assessForm.assessTime !== "string" ? this.formatAssessTime(this.assessForm.assessTime) : "",
        pgfs: this.assessForm.assessMethod,
        fxdj: this.formatRiskLevel()
      };
      if(insertData.pgfs === this.lecLabel) {
        insertData["lpgfz"] = this.lec.l;
        insertData["epgfz"] = this.lec.e;
        insertData["cpgfz"] = this.lec.c;
      } else if(insertData.pgfs === this.lsLabel) {
        insertData["lpgfz"] = this.ls.l;
        insertData["spgfz"] = this.ls.s;
      }
      addRiskAssess(insertData).then(res => {
        if(res.data.success) {
          this.loadSaveRiskAssess = false;
          this.$emit('saveRiskAssessSuccess');
        }
      });
    },

    // 获取评估时间
    formatAssessTime(date) {
      var year = date.getFullYear();
      var month = this.getFullNum(date.getMonth() + 1);
      var day = this.getFullNum(date.getDate());
      return year + "-" + month + "-" + day;
    },

    // 补全时间
    getFullNum(num) {
      if (num < 10) {
        return '0' + num
      }
      return num
    },

    // 获取风险等级
    formatRiskLevel() {
      if(this.assessForm.assessMethod === this.lecLabel) {
        let lecValue = this.formatRiskLevelValue( this.lec.l * this.lec.e * this.lec.c );
        if(lecValue >= 320) {
          return "1";
        } else if(lecValue >= 160 && lecValue <= 319) {
          return "2";
        } else if(lecValue >= 71 && lecValue <= 159) {
          return "3";
        } else if(lecValue <= 70) {
          return "4"
        }
      } else if(this.assessForm.assessMethod === this.lsLabel) {
        let lsValue = this.formatRiskLevelValue( this.ls.l * this.ls.s );
        if(lsValue >= 17) {
          return "1";
        } else if(lsValue >= 13 && lsValue <= 16) {
          return "2";
        } else if(lsValue >= 9 && lsValue <= 12) {
          return "3";
        } else if(lsValue <= 8) {
          return "4"
        }
      }
    },

    // 格式化风险等级值
    formatRiskLevelValue(value) {
      if ((value + "").indexOf(".") < 0) {
        return value;
      } else {
        var data = "" + value;
        var before = data.substring(0, data.indexOf("."));
        var after = data.substring(data.indexOf(".") + 1, data.indexOf(".") + 3);
        return before + "." + after;
      }
    },

    // 取消保存风险评估
    cancelSaveRiskAssess() {
      this.$emit('cancelSaveRiskAssess');
    },

    // 关闭弹窗
    handleCloseRiskAssess(done) {
      done();
      this.$emit('handleCloseRiskAssess');
    }

  }
}
</script>